package com.seckill.controller;

import com.seckill.common.error.BusinessException;
import com.seckill.common.error.ErrorEnum;
import com.seckill.common.model.CommonResult;
import com.seckill.common.service.RedisService;
import com.seckill.model.dto.UserDTO;
import com.seckill.model.vo.UserVO;
import com.seckill.service.UserService;
import com.seckill.util.MdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户
 *
 * @author lill
 * @date 2019-06-04
 */

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService<String> stringRedisService;

    @Autowired
    private RedisService<UserDTO> userDTORedisService;

    @RequestMapping("/getUser")
    public CommonResult getUser(@RequestParam("id") Integer id) {
        UserDTO userDTO = userService.getUserById(id);
        if (null == userDTO) {
            return CommonResult.success(null);
        }
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(userDTO, userVO);
        return CommonResult.success(userVO);
    }

    @RequestMapping(value = "/getOpt", method = {RequestMethod.POST})
    public CommonResult getOpt(@RequestParam("telephone") String telephone) {
        userService.getOpt(telephone);
        return CommonResult.success();
    }

    @RequestMapping(value = "/register", method = {RequestMethod.POST})
    public CommonResult register(@RequestParam(name = "telephone") String telephone,
                                 @RequestParam(name = "otpCode") String otpCode,
                                 @RequestParam(name = "name") String name,
                                 @RequestParam(name = "gender") Byte gender,
                                 @RequestParam(name = "age") Integer age,
                                 @RequestParam(name = "password") String password) {
        //验证手机号和对应otpCode相符
        if (!stringRedisService.exists(telephone)) {
            throw new BusinessException(ErrorEnum.VALIDATION_REQUEST_PARAM_ERROR, "短信验证码不正确");
        }
        String redisOptCode = stringRedisService.getCache(telephone);
        if (!StringUtils.equals(otpCode, redisOptCode)) {
            throw new BusinessException(ErrorEnum.VALIDATION_REQUEST_PARAM_ERROR, "短信验证码不正确");
        }
        //用户注册流程
        UserDTO userDTO = new UserDTO();
        userDTO.setName(name);
        userDTO.setGender(gender);
        userDTO.setAge(age);
        userDTO.setTelephone(telephone);
        userDTO.setRegisterMode("byphone");
        userDTO.setEncryptPassword(MdUtil.encodeUpperCase32(password));
        userService.register(userDTO);
        return CommonResult.success();

    }


    @RequestMapping(value = "/login", method = {RequestMethod.POST})
    public CommonResult login(@RequestParam("telephone") String telephone,
                              @RequestParam("password") String password) {
        // 校验用户登录信息
        UserDTO userDTO = userService.validateLogin(telephone, password);
        // 将登陆凭证加入到用户成功登录的redis中
        //生成 token, uuid
        String token = UUID.randomUUID().toString().replace("-", "");
        userDTORedisService.setCache("user_" + token, userDTO, 1, TimeUnit.HOURS);
        System.out.println("user_" + token + "  " + userDTORedisService.getCache("user_" + token));
        return CommonResult.success(token);
    }
}
